package myzd.api.controllers;

import io.swagger.annotations.*;
import libedge.annotations.Authentication;
import libedge.domain.exceptions.GenericException;
import libedge.domain.request.ResultWrapper;
import lombok.extern.slf4j.Slf4j;
import myzd.api.domain.ListResult;
import myzd.api.domain.PagedResult;
import myzd.api.domain.TreeResult;
import myzd.domain.enums.ServiceTypeList;
import myzd.domain.response.*;
import myzd.services.DoctorKbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zks
 * @since 2017/10/18 18:31
 */
@RestController
@Slf4j
@Api(description = "省市区县相关接口")
public class DoctorKbController {

  @Autowired
  private DoctorKbService doctorKbService;

  @GetMapping("api/v1/hospitals")
  @ApiOperation(value = "搜索医院(IMS3.0)", produces = "application/json")
  @Authentication
  @ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "Session Id", required = true, dataType = "string", paramType = "header")
  })
  public ResultWrapper<PagedResult<Hospital>> searchHospital(
    @ApiParam("医院名(模糊搜索)") @RequestParam(value = "name", required = false) String name,
    @ApiParam(value = "页码", defaultValue = "1") @RequestParam(value = "page", required = false) Integer page,
    @ApiParam(value = "每页条目数量", defaultValue = "10") @RequestParam(value = "page_size", required = false) Integer pageSize
  ) throws GenericException {
    if (page == null || page < 1) {
      page = 1;
    }
    if (pageSize == null || pageSize < 1) {
      pageSize = 10;
    }
    return new ResultWrapper<>(doctorKbService.searchHospitalByName(name, page, pageSize));
  }

  @GetMapping("api/v1/diseases")
  @ApiOperation(value = "搜索疾病(IMS3.0)", produces = "application/json")
  @Authentication
  @ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "Session Id", required = true, dataType = "string", paramType = "header")
  })
  public ResultWrapper<PagedResult<Disease>> searchDisease(
    @ApiParam("疾病名") @RequestParam(value = "name", required = false) String name,
    @ApiParam(value = "页码", defaultValue = "1") @RequestParam(value = "page", required = false) Integer page,
    @ApiParam(value = "每页条目数量", defaultValue = "10") @RequestParam(value = "page_size", required = false) Integer pageSize
  ) throws GenericException {
    if (page == null || page < 1) {
      page = 1;
    }
    if (pageSize == null || pageSize < 1) {
      pageSize = 10;
    }
    return new ResultWrapper<>(doctorKbService.searchDiseaseByDiseaseName(name, page, pageSize));
  }

  @GetMapping("api/v1/doctors")
  @ApiOperation(value = "搜索医生(IMS3.0 & IMS3.1)", notes = "IMS3.1新增查询参数“service_type”，即根据服务类型搜索专家信息", produces = "application/json")
  @Authentication
  @ApiImplicitParams({
    @ApiImplicitParam(name = "Authorization", value = "Session Id", required = true, dataType = "string", paramType = "header")
  })
  public ResultWrapper<PagedResult<DoctorDetailsDto>> searchDoctor(
    @ApiParam("医生名") @RequestParam(value = "name", required = false) String name,
    @ApiParam("医生医院名称") @RequestParam(value = "hospital_name", required = false) String hospitalName,
    @ApiParam("疾病名") @RequestParam(value = "disease_name", required = false) String diseaseName,
    @ApiParam("一级分类ID") @RequestParam(value = "parent_category_id", required = false) Integer parentCategoryId,
    @ApiParam("二级分类ID") @RequestParam(value = "category_id", required = false) Integer categoryId,
    @ApiParam("城市ID") @RequestParam(value = "city_id", required = false) Integer cityId,
    @ApiParam(value = "服务类型") @RequestParam(value = "service_type", required = false) ServiceTypeList serviceType,
    @ApiParam(value = "页码", defaultValue = "1") @RequestParam(value = "page", required = false) Integer page,
    @ApiParam(value = "每页条目数量", defaultValue = "10") @RequestParam(value = "page_size", required = false) Integer pageSize
  ) throws GenericException {
    if (page == null || page < 1) {
      page = 1;
    }
    if (pageSize == null || pageSize < 1) {
      pageSize = 10;
    }
    return new ResultWrapper<>(
      doctorKbService.searchDoctors(name, hospitalName, diseaseName, parentCategoryId, categoryId, cityId, serviceType, page, pageSize));
  }

  @GetMapping(path = "api/v1/diseaseCategories/listTreeNode")
  @ApiOperation(value = "疾病类别信息集合(IMS3.0)", notes = "疾病类别信息集合", produces = "application/json")
  public ResultWrapper<ListResult<TreeResult<DiseaseCategory>>> listTreeNode() throws GenericException {
    return new ResultWrapper<>(doctorKbService.searchAllDiseaseCategory());
  }


}
